<template>
	<div>
    <div style="margin-bottom: 20px">
      <el-button type="primary" @click="templateAdd()"><i class="el-icon-plus"></i>商品活动</el-button>
      <!-- <el-button type="primary" @click="setSortDialog = true"><i class="el-icon-plus"></i>排序</el-button> -->
    </div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" label-width="70px">
      <el-form-item label="分公司">
        <el-select v-if="!brachDisabled" v-model="formInline.branchId" :disabled="brachDisabled" style="width:160px">
          <el-option v-for="(item, index) in this.branchList" :value="item.branch_Id" :label="item.branch_Name" :key="index"></el-option>
        </el-select>
        <el-input v-if="brachDisabled" v-model="showBranch" :disabled="brachDisabled" style="width:160px"></el-input>
      </el-form-item>
      <el-form-item label="活动名称">
        <el-input v-model="formInline.name" placeholder="请输入名称" style="width:160px"></el-input>
      </el-form-item>
      <!-- <el-form-item label="状态">
        <el-select v-model="formInline.state" placeholder="状态" clearable style="width:160px">
          <el-option label="草稿" value="0"></el-option>
          <el-option label="待审核" value="1"></el-option>
          <el-option label="审核通过" value="2"></el-option>
          <el-option label="审核不通过" value="3"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item  style="float:right">
        <el-button type="primary" @click="searchList">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="isLoading" :data="tableData" style="width: 100%" border>
      <el-table-column prop="id" label="主键" align="center" width="100"></el-table-column>
      <!-- <el-table-column label="排序" align="center"></el-table-column> -->
      <el-table-column prop="activityName" label="活动名称" align="center"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
      <el-table-column prop="state" label="状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template scope="scope">
          <el-button v-if="scope.row.state === '草稿' || scope.row.state === '待审核' || scope.row.state === '审核不通过' || scope.row.state === '审核通过'" @click="handle(scope.$index, scope.row, $event)" size="small">详情</el-button>
          <el-button v-if="scope.row.state === '草稿' || scope.row.state === '待审核' || scope.row.state === '审核不通过' || scope.row.state === '审核通过'" @click="handle(scope.$index, scope.row, $event)" size="small">预览</el-button>
          <el-button v-if="scope.row.state === '待审核'" @click="handle(scope.$index, scope.row, $event)" size="small">审核</el-button>
          <el-button v-if="scope.row.state === '草稿' || scope.row.state === '审核通过'" @click="handle(scope.$index, scope.row, $event)" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      @current-change="handleCurrentChange"
      :page-size="Number(formInline.pageSize)"
      layout="total, prev, pager, next"
      :total="Number(formInline.total)"
      style="padding-top:10px;text-align: right;">
    </el-pagination>
    <el-dialog title="审核信息" :visible.sync="checkDialog" size="tiny">
      <p></p>
      <span>审核状态：</span>
      <template>
        <el-radio class="radio" v-model="audit.auditType" label="1">审核通过</el-radio>
        <el-radio class="radio" v-model="audit.auditType" label="2">不通过</el-radio>
      </template>
      <el-input style="margin-top: 20px" v-model="audit.auditOpinion" placeholder="请输入审核意见"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="checkDialog = false">关 闭</el-button>
        <el-button type="primary" @click="updateAudit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="跳转动作详情" :visible.sync="coupontDeteil">
      <el-form label-width="180px">
        <div class="detail-info" style="margin-top: 43px">
          <div class="form-item">
            <el-form-item label="活动ID">
              <span>{{this.detailData.id}}</span>
            </el-form-item>
            <el-form-item label="活动名称">
              <span>{{this.detailData.activityName}}</span>
            </el-form-item>
            <el-form-item label="背景颜色">
              <span :style="colorStyle"></span>
            </el-form-item>
            <el-form-item label="BANNER图片">
              <img :src="imgHost + '/' + this.detailData.bannerImage" class="avatar">
            </el-form-item>
            <!-- <el-form-item label="关联券活动ID">
              <span>{{this.detailData.cadname}}</span>
            </el-form-item>
            <el-form-item label="关联券活动名称">
              <span>{{this.detailData.cadname}}</span>
            </el-form-item> -->
            <el-form-item label="活动链接">
              <span>{{this.detailData.activityLink}}</span>
            </el-form-item>
            <el-form-item label="状态">
              <span>{{this.detailData.stateName}}</span>
            </el-form-item>
            <el-form-item label="创建时间">
              <span>{{this.detailData.createTime}}</span>
            </el-form-item>
            <el-form-item label="创建人">
              <span>{{this.detailData.createUser}}</span>
            </el-form-item>
            <el-form-item label="编辑时间">
              <span>{{this.detailData.updateTime}}</span>
            </el-form-item>
            <el-form-item label="编辑人">
              <span>{{this.detailData.updateUser}}</span>
            </el-form-item>
            <el-form-item label="审核时间">
              <span>{{this.detailData.auditTime}}</span>
            </el-form-item>
            <el-form-item label="审核人">
              <span>{{this.detailData.auditUser}}</span>
            </el-form-item>
          </div>
        </div>
        <el-table :data="this.detailData.productActivityInfos" border>
          <el-table-column label="分类标题" prop="blockName" align="center"></el-table-column>
          <el-table-column label="商品排列样式" align="center">
            <template scope="scope">
              <span v-if="scope.row.blockDisplay === 1">一列</span>
              <span v-if="scope.row.blockDisplay === 2">二列</span>
            </template>
          </el-table-column>
          <el-table-column label="关联类型" align="center">
            <template scope="scope">
              <span v-if="scope.row.relateType === 1">关联内容</span>
              <span v-if="scope.row.relateType === 2">关联满赠活动</span>
              <span v-if="scope.row.relateType === 3">关联满减活动</span>
            </template>
          </el-table-column>
          <el-table-column label="内容/活动ID" prop="contentId" align="center"></el-table-column>
          <el-table-column label="内容/活动名称" prop="relateName" align="center"></el-table-column>
        </el-table>
        <!-- <div class="detail-info" style="margin-top: 43px">
          <div class="form-item">
            <h4>PC订货</h4>
            <el-form-item label="图片">
            </el-form-item>
            <el-form-item label="跳转方式">
              <span>{{this.detailData.detail[0].actionname}}</span>
            </el-form-item>
          </div>
        </div> -->
      </el-form>
    </el-dialog>
	</div>
</template>
<script>
  import { hdljpz, getInfo } from '../../config/proxy.js'
  import mixins from '../../components/mixins.vue'
  import { apiHost } from '../../config/settings.js'
  export default {
    mixins: [mixins],
    data () {
      return {
        formInline: {
          branchId: '', // 分公司id
          name: '', // 名称
          state: '1', // 状态
          pageSize: 10,
          // sortType: '1', // 排序类型
          page: 1,
          total: ''
        },
        showBranch: '', // 展示分公司
        brachDisabled: false,
        coupontDeteil: false,
        branchList: [],
        couponInfoList: [],
        checkInfoList: [],
        departmentList: [],
        tableData: [],
        isLoading: false,
        isLoading2: false,
        detailData: '',
        detailDialog: false,
        sortInfo: {
          usertype: '', // 客资类型
          branchId: '',
          equipment: '',
          sortType: '2',
          page: 1,
          pageSize: 1000
        },
        setSortDialog: false, // 排序弹框
        customerTypeLoading: false, // 客资类型加载
        customerTypeList: [], // 客资类型数组
        setSortTableList: [],
        colorStyle: {
          width: '16px',
          height: '16px',
          display: 'inline-block',
          border: '1px solid #000',
          background: '',
          marginTop: '8px'
        },
        checkDialog: false,
        audit: { // 审核
          auditType: '1', // 审核类型
          auditOpinion: '' // 审核意见
        }
      }
    },
    methods: {
      templateAdd () { // 新增款台广告
        this.openNewWindow('sphdAdd', '新增商品活动', 'sphdAdd')
      },
      searchAd () { // 查询广告
        var _data = {
          system: '2',
          page: 1,
          pageSize: 10000
        }
        hdljpz.sphd.list(_data, (res) => { // 查询
          console.log(res)
          if (res.message === 'ok') {
            this.markidGroup = res.content.content
          } else {
            this.message('查询失败', 'error')
          }
        })
      },
      searchCustomerType () { // 查询客资类型
        var _data = ''
        getInfo.client(_data, (res) => {
          console.log(res)
          if (res.message === 'ok') {
            this.customerTypeList = res.content.content
            this.customerTypeLoading = false
          }
        })
      },
      searchList () { // 查询
        // this.tableData = []
        this.isLoading = true
        var _data = this.formInline
        console.log(_data)
        hdljpz.sphd.list(_data, (res) => { // 查询
          if (res.message === 'ok') {
            // console.log(res.content === null)
            console.log(res)
            this.isLoading = false
            this.tableData = res.content !== null ? res.content.content : []
            this.formInline.total = res.content !== null ? res.content.count : 0
            this.formInline.page = res.content !== null ? res.content.page : 0
            this.matchState(res.content)
          } else {
            this.message('查询失败', 'error')
          }
        })
      },
      matchState (res) { // 匹配状态
        if (res) {
          for (var i = 0; i < res.content.length; i++) {
            if (res.content[i].state === 0) { res.content[i].state = '草稿' }
            if (res.content[i].state === 1) { res.content[i].state = '待审核' }
            if (res.content[i].state === 2) { res.content[i].state = '审核通过' }
            if (res.content[i].state === 3) { res.content[i].state = '审核不通过' }
          }
        }
      },
      handleCurrentChange (val) { // 分页
        this.formInline.page = val
        this.tableData = []
        this.isLoading = true
        this.searchList()
      },
      handle (index, row, event) { // 操作
        const handleName = event.target.innerText
        switch (handleName) {
          case '详情':
            console.log(row.id)
            hdljpz.sphd.detail({id: row.id}, (res) => {
              console.log(res, '详情')
              if (res.message === 'ok') {
                this.coupontDeteil = true
                this.detailData = res.content
                this.colorStyle.background = res.content.backgroundColor
              } else {
                this.message('查看详情', 'error')
              }
            })
            // this.openNewWindow('zcfqDetail', '注册返券详情', 'zcfqDetail', row.id)
            break
          case '编辑':
            this.openNewWindow('sphdbj', '编辑商品活动', 'sphdAdd', row)
            break
          case '审核':
            this.audit.auditResult = '9'
            this.audit.reason = ''
            this.checkDialog = true
            this.row = row
            break
          case '预览':
            var newWindow = window.open()
            hdljpz.sphd.preview({id: row.id}, (res) => {
              if (res.message === 'ok') {
                newWindow.location = res.content
              }
            })
            break
          //   this.buttonPublicMethods('审核通过', 'auditOk', row)
          //   break
          // case '审核不通过':
          //   this.buttonPublicMethods('审核不通过', 'auditNo', row)
          //   break
        }
      },
      updateAudit () { // 审核
        if (this.audit.auditType === '2') {
          if (this.audit.auditOpinion === '') {
            this.message('请填写审核意见', 'error')
          } else {
            this.goAudit()
          }
        } else {
          this.goAudit()
        }
      },
      goAudit () {
        var _data = this.audit
        _data.id = this.row.id
        _data.auditUser = this.userName
        console.log(_data)
        hdljpz.sphd.audit(_data, (res) => {
          console.log(res)
          if (res.message === 'ok') {
            this.searchList()
            this.message('审核成功', 'success')
            this.checkDialog = false
            this.$store.dispatch('sphdState', this.$store.state.updateState.sphdState + 1)
          } else {
            this.message('审核失败', 'error')
          }
        })
      },
      buttonPublicMethods (text, fnNane, row) { // 删除 撤回 作废 公共方法
        this.$confirm('您确定要' + text + '吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var _data = {}
          _data.id = row.id
          _data.auditUser = this.userName
          // console.log(_data)
          // console.log(fnNane)
          hdljpz.sphd[fnNane](_data, (res) => {
            console.log(res)
            if (res.message === 'ok') {
              this.searchList()
              this.message(text + '成功', 'success')
              this.$store.dispatch('sphdState', this.$store.state.updateState.sphdState + 1)
            } else {
              this.message(text + '失败', 'error')
            }
          })
        }).catch(() => {
        })
      },
      openNewWindow (menuId, menuName, perController, uid) { // 新增tab方法
        var _data = {
          menu_id: menuId,
          menu_name: menuName,
          per_controller: perController
        }
        if (uid) {
          _data.uid = uid
        }
        this.$store.dispatch('addTab', _data)
      },
      timeDeltSecond (d) { // 时间去秒
        if (d) {
          return d.split(' ')[0]
        }
      },
      daterangeFormat (t) { // 时间段格式化
        console.log(t.split(' - '))
        var tGroup = t.split(' - ')
        this.formInline.offlinetime = tGroup[1]
        this.formInline.orderuptime = tGroup[0]
      },
      message (text, type) { // 消息提示
        this.$message({
          showClose: true,
          message: text,
          type: type
        })
      }
    },
    computed: {
      update_state: function () {
        // console.log(state.updateState.dhmState)
        return this.$store.state.updateState.sphdState
      },
      userName: function () {
        if (!this.$store.state.user.info.user_name) {
          this.$store.dispatch('SET_TOKEN')
        }
        return this.$store.state.user.info.user_name
      },
      userId: function () {
        return this.$store.state.user.info.roleid
      },
      branchName: function () {
        return this.$store.state.user.info.branchId === 1 ? '总公司' : this.$store.state.user.info.branchName
      },
      branchId: function () {
        return this.$store.state.user.info.branchId
      },
      deptName: function () {
        return this.$store.state.user.info.parOrgName
      },
      deptId: function () {
        return this.$store.state.user.info.or_org_id
      }
    },
    watch: {
      update_state (curVal, oldVal) {  // 监听 更新状态 查询list
        this.searchList()
      }
    },
    created () {
      // this.searchAd() // 查询广告
      this.imgHost = apiHost.imgHost
      this.searchCustomerType()
      if (this.branchId === 1) {
        this.brachDisabled = false
        this.formInline.branchId = this.branchId
        this.sortInfo.branchId = this.branchId
      } else {
        this.brachDisabled = true
        this.showBranch = this.branchName
        this.sortInfo.branchId = this.branchId
        this.formInline.branchId = this.branchId
      }
      this.searchList()
    }
  }
</script>
<style>
.line{
	margin-left:20px;
	text-align:center
}
.el-button+.el-button{
  margin-left: 0
}
.el-table .cell, .el-table th>div{
  padding-left:10px;
  padding-right:10px;
}
.el-dialog--small{
  width:60%
}
</style>
